<?php

namespace App\Http\Service;

use App\Models\Viewcountmore;
use App\Models\Doubanpost;
use App\Models\Baoliaopost;
use App\Http\Service\BaseService;
use App\Http\Service\ChatgptService;
use App\Http\Service\UserService;
use App\Http\Service\SiteConfigService;
use App\Models\Baoliaopostback;
class ViewcountmoreService  extends BaseService
{
    private $model;
    public  $userService;
    public function __construct(Viewcountmore $model,ChatgptService $chatgptService,UserService  $userService,SiteConfigService $siteConfigService,Doubanpost $doubanpost,Baoliaopost $baoliaopost,Baoliaopostback $baoliaopostback)
    {
        parent::__construct();
        $this->model = $model;
        $this->chatgptService=$chatgptService;
        $this->userService=$userService;
        $this->siteConfigService=$siteConfigService;
        $this->doubanpost=$doubanpost;
        $this->baoliaopost=$baoliaopost;
        $this->baoliaopostback=$baoliaopostback;

       
    }

    
    #添加多个浏览记录 
    public function addData($request,$data=[])
    {
        $parameter=$data;
        $table_type=$data['table_type'];
        unset($data['table_type']);
        $data['today_time']=date("Y-m-d");

        if(isset($data['unit_price'])&&empty($data['unit_price'])){
            $data['unit_price']=0.00;
        }
        if(isset($data['commission_amount'])&&empty($data['commission_amount'])){
            $data['commission_amount']=0.00;
        }

        //dd($data);
        $insertId=$this->model->insertGetId($data);
        #type =1 逛逛  type =2  爆料
        $type=$data['type'];
        $type_status='2';
        $default_view_count =0;
        $first=[];
        if($type=='2'){
            #爆料 baoliaopost
            if($table_type=='list'){
                $first=$this->baoliaopost->where(['unique_id'=>$data['unique_id']])->first();
            }else{
                $first=$this->baoliaopostback->where(['unique_id'=>$data['unique_id']])->first();

            }
            

        }else{
            #逛逛
            /*$first=$this->doubanpost->where(['unique_id'=>$data['unique_id']])->first();*/
            $first['type']=2;
            $first['view_count']=0;
        }

        $type_status=$first['type'];
        //dd($type_status);
        #查询默认值
        $siteConfig=$this->siteConfigService->getList('4');
        
        
        if(in_array($type_status,['2','4'])){
            #$default_view_count=$siteConfig['content']['view_count'];
            /*$min=$siteConfig['content']['min'];
            $max=$siteConfig['content']['max'];
            $default_view_count=getRandomInRange($min,$max);
            */
            if(isset($first['view_count'])){
                $default_view_count=$first['view_count'];
            }
        }

        
        //dd($default_view_count);

        #计算数量 -爆料 -逛逛
        $where=[
            'unique_id'=>$data['unique_id'],
            'people_id'=>$data['people_id'],
            'type'=>$data['type'],
        ];

        //dd($where);

        $view_count=$this->model->where($where)->count();
        //dd($view_count);
        if(empty($view_count)){
            $view_count=0;
        }
        $num=$view_count+$default_view_count;
        

        //dd($num);
        if($type=='1'){
             $this->doubanpost->where(['unique_id'=>$data['unique_id']])->update(['view_count'=>$num]);
        }else{
            if($table_type=='list'){
                $this->baoliaopost->where(['unique_id'=>$data['unique_id']])->update(['view_count'=>$num]);
            }else{
                $this->baoliaopostback->where(['unique_id'=>$data['unique_id']])->update(['view_count'=>$num]);
            }
        }
        
    
        return true;
        
        
    }
  
}
